package com.example.docking.controller;

import com.example.docking.dao.jindie.GenericDataItem;
import com.example.docking.dao.jindie.ResponseData;
import com.example.docking.dao.jindie.response.OrderList;
import com.example.docking.service.impl.DataServiceImpl;
import com.example.docking.util.Constants;
import com.example.docking.util.TimeRangeMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.smecloud.apigw.model.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class PurInboundController {
    private static final Logger logger = LoggerFactory.getLogger(SalesOrderController.class);
    private DataServiceImpl Service = new DataServiceImpl();
    private Gson gson = new Gson();
    private HashMap<String, String> map = new HashMap<>();

    public List<OrderList> selectOrderList() {
        List<OrderList> res = new ArrayList<>();
        int page = 1; // 初始页码
        int pageSize = 100; // 每页记录数
        map.put("page", String.valueOf(page));
        map.put("page_size", String.valueOf(pageSize));
        TimeRangeMap timeRangeMap = new TimeRangeMap();
        map.put("create_start_time", timeRangeMap.getDayStartTime()); //    创建时间-开始时间的时间戳(毫秒)
        map.put("create_end_time", timeRangeMap.getDayEndTime()); //  	创建时间-结束时间的时间戳(毫秒)
        map.put("bill_status", "C");//单据状态（所有：“”，已审核：“C”，未审核：“Z”）
        try {
            // 查询采购入库单数据  判断
            ApiResult OrderListDataResult = Service.SelectDataList(Constants.PUR_INBOUND, map, "");
            Type OrderListDataType = new TypeToken<ResponseData<GenericDataItem<OrderList>>>() {
            }.getType();
            ResponseData<GenericDataItem<OrderList>> responseSaleData = gson.fromJson(OrderListDataResult.getBody(), OrderListDataType);
            GenericDataItem<OrderList> genericDataItemOrderList = responseSaleData.getData();
            // 解析分页信息
            String count = genericDataItemOrderList.getCount();
            int countNum = Integer.valueOf(count);
            int Pagesum = pageSize;
            List<OrderList> OrderListData = genericDataItemOrderList.getRows();

            res = new ArrayList<>(OrderListData);
            // 如果不是最后一页，继续获取下一页数据
            while (Pagesum < countNum) {
                page++;
                map.put("page", String.valueOf(page));
                OrderListDataResult = Service.SelectDataList(Constants.SAL_ORDER, map, "");
                responseSaleData = gson.fromJson(OrderListDataResult.getBody(), OrderListDataType);
                genericDataItemOrderList = responseSaleData.getData();
                OrderListData = genericDataItemOrderList.getRows();
                res.addAll(OrderListData);
                Pagesum = (pageSize * page);
            }

            logger.info("采购入库单：res: {}", res);
        } catch (Exception e) {
            logger.error("查询采购入库单：Error occurred while selecting pur inbound", e);
            throw new RuntimeException(e);
        }
        return res;
    }

    public static void main(String[] args) {
        PurInboundController controller = new PurInboundController();
        controller.selectOrderList();


    }
}
